﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinkCard.Model;

namespace LinkCard.Dao
{
    public class SupplierDao:ITopLinkDao
    {
        public int Add(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objAdd = (Supplier)obj;
                db.AddToSupplier(objAdd);
                rs = db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public int Update(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objUpdate = (Supplier)obj;
                var id = objUpdate.SupplierID;
                var oldObj = db.Supplier.FirstOrDefault(t => t.SupplierID == id);
                if (oldObj != null)
                {
                    oldObj.Director = objUpdate.Director;
                    oldObj.Name = objUpdate.Name;
                    oldObj.Address = objUpdate.Address;
                    oldObj.Phone = objUpdate.Phone;
                    oldObj.Fax = objUpdate.Fax;
                    oldObj.Email = objUpdate.Email;
                    oldObj.TaxCode = objUpdate.TaxCode;
                    oldObj.ContactPersonName = objUpdate.ContactPersonName;
                    oldObj.ContactPersonMail = objUpdate.ContactPersonMail;
                    oldObj.Business = objUpdate.Business;
                    if (objUpdate.AuthorizationStatement != null)
                        oldObj.AuthorizationStatement = objUpdate.AuthorizationStatement;
                    oldObj.ContractNumber = objUpdate.ContractNumber;
                    if (objUpdate.ContractStatement != null)
                        oldObj.ContractStatement = objUpdate.ContractStatement;
                    oldObj.Status = objUpdate.Status;
                    oldObj.Version = objUpdate.Version;
                    rs = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public int Delete(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objDel = (Supplier)obj;
                var id = objDel.SupplierID;
                var oldObj = db.Supplier.FirstOrDefault(t => t.SupplierID == id);
                if (oldObj != null)
                {
                    db.DeleteObject(oldObj);
                    rs = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public Supplier GetById(long id)
        {
            var ct = new Supplier();
            try
            {
                var db = new TopLinkEntities();
                ct = db.Supplier.FirstOrDefault(t => t.SupplierID == id);
            }
            catch (Exception)
            {
                throw;
            }
            return ct;
        }

        public List<Supplier> GetAll()
        {
            var lst = new List<Supplier>();
            try
            {
                var db = new TopLinkEntities();
                lst = db.Supplier.ToList();
            }
            catch (Exception)
            {
                throw;
            }
            return lst;
        }

        public List<Supplier> GetSupplierByTag(string tag, int page, int pagesize, out int pagecount)
        {
            using(TopLinkEntities db=new TopLinkEntities())
            {
                int suppliesCount = db.Supplier.Where(q => q.Status == 2 && ("," + q.Tags.Trim().ToLower() + ",").Replace(", ", ",").Replace(" ,", ",").Contains("," + tag.Trim().ToLower() + ",")).Count();

                //int skip = currentPage*pageSize;
                int skip = (page - 1) * pagesize;
                var lst = db.Supplier.Where(q => q.Status == 2 && ("," + q.Tags.Trim().ToLower() + ",").Replace(", ", ",").Replace(" ,", ",").Contains("," + tag.Trim().ToLower() + ",")).OrderBy(s => s.Name).Skip(skip).Take(pagesize).ToList();
                
                
                if (suppliesCount % pagesize == 0)
                    pagecount = suppliesCount / pagesize;
                else
                    pagecount = Convert.ToInt32(suppliesCount / pagesize) + 1;

                return lst;
            }
        }

        // nham.nguyen
        public List<Supplier> GetListPaged(int currentPage, int pageSize, ref int pageCount, int supplierCateID)
        {
            var lst = new List<Supplier>();
            try
            {
                var db = new TopLinkEntities();
                // Count pages
                int suppliesCount = db.Supplier.Count();

                //int skip = currentPage*pageSize;
                int skip = (currentPage - 1) * pageSize;
                lst = db.Supplier.Where(s=>s.Status==2 && s.SupplierCateID == supplierCateID && s.SupplierTransparency == false).OrderBy(s=>s.SupplierOrder).Skip(skip).Take(pageSize).ToList();
                
                
                if (suppliesCount % pageSize == 0)
                    pageCount = suppliesCount / pageSize;
                else
                    pageCount = Convert.ToInt32(suppliesCount / pageSize) + 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return lst;
        }
        // hoan.ha
        public List<Supplier> GetListPaged()
        {
            var lst = new List<Supplier>();
            try
            {
                var db = new TopLinkEntities();
                // Count pages
                int suppliesCount = db.Supplier.Count();

                //int skip = currentPage*pageSize;
                //int skip = (currentPage - 1) * pageSize;
                lst = db.Supplier.Where(s => s.Status == 2 && s.LogoHome != "").OrderBy(s => s.SupplierOrder).ToList();


                //if (suppliesCount % pageSize == 0)
                //    pageCount = suppliesCount / pageSize;
                //else
                //    pageCount = Convert.ToInt32(suppliesCount / pageSize) + 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return lst;
        }
    }
}
